const fs = require('fs')
const path = require('path')

const pkg = require('./package.json')

const version = pkg.version
const DIST = './dist/win-unpacked/'
const SAVES = ['ffmpeg.dll', 'icudtl.dat', 'resources.pak', 'v8_context_snapshot.bin']
const COPYS = ['万能话术.txt', '吸粉话术.txt', '成交话术.txt', '通用话术.txt']
const NEWDIST = `./dist/优淘V${version}-淘宝直播场控（全网独家首发）淘宝直播营销必备工具、助力主播上推荐、软件招收实力代理`

const delDir = (path) => {
  var files = fs.readdirSync(path)
  for (let item of files) {
    var stats = fs.statSync(`${path}/${item}`)
    if (stats.isDirectory()) {
      delDir(`${path}/${item}`)
    } else {
      fs.unlinkSync(`${path}/${item}`)
    }
  }
  fs.rmdirSync(path)
}

var files = fs.readdirSync(DIST)

files.forEach((item) => {
  var dir = `${DIST}/${item}`
  var stats = fs.statSync(dir)
  var info = path.parse(dir)
  if (!stats.isDirectory() && info.ext != '.exe' && SAVES.indexOf(item) == -1) {
    fs.unlinkSync(dir)
  }
})
COPYS.forEach((item) => {
  fs.copyFileSync(`./hs/${item}`, `${DIST}${item}`)
})
if (fs.existsSync(NEWDIST)) {
  delDir(NEWDIST)
}

fs.renameSync(DIST, NEWDIST)
